package com.zhang.first.day07;

import com.zhang.first.utils.ClickEvent;
import com.zhang.first.utils.ClickEventSource;
import com.zhang.first.utils.UrlViewCountPerWindow;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用KeyedProcessFunction
 *
 * @title: 实现滑动窗口
 * @author: zhang
 * @date: 2022/1/21 23:44
 */
public class Example5 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickEventSource())
                .keyBy(r -> r.url)
                .process(new KeyedProcessFunction<String, ClickEvent, UrlViewCountPerWindow>() {
                    private MapState<Long, List<ClickEvent>> mapState;

                    private long slidingWindowSize = 10 * 1000L;
                    private long slidingWindowStep = 5 * 1000L;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        mapState = getRuntimeContext().getMapState(
                                new MapStateDescriptor<Long, List<ClickEvent>>(
                                        "map",
                                        Types.LONG,
                                        Types.LIST(Types.POJO(ClickEvent.class))
                                )
                        );
                    }

                    @Override
                    public void processElement(ClickEvent value, Context ctx, Collector<UrlViewCountPerWindow> out) throws Exception {
                        //计算窗口
                        long currTime = ctx.timerService().currentProcessingTime();
                        ArrayList<Long> starts = new ArrayList<>();
                        long lastStartTime = getWindowStart(currTime, slidingWindowStep);
                        for (long start = lastStartTime; start > currTime - slidingWindowSize; start -= slidingWindowStep) {
                            starts.add(start);
                        }

                        for (Long start : starts) {
                            if (!mapState.contains(start)) {
                                ArrayList<ClickEvent> events = new ArrayList<>();
                                events.add(value);
                                mapState.put(start, events);
                            } else {
                                mapState.get(start).add(value);
                            }

                            ctx.timerService().registerProcessingTimeTimer(start + slidingWindowSize - 1L);
                        }
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<UrlViewCountPerWindow> out) throws Exception {
                        long windowEndTime = timestamp + 1L;
                        long windowStartTime = windowEndTime - slidingWindowSize;
                        String url = ctx.getCurrentKey();
                        long count = mapState.get(windowStartTime).size();
                        out.collect(
                                new UrlViewCountPerWindow(
                                        url,
                                        count,
                                        windowStartTime,
                                        windowEndTime
                                )
                        );
                        mapState.remove(windowStartTime);
                    }
                })
                .print();

        env.execute();
    }


    public static long getWindowStart(long timestamp, long step) {
        return timestamp - (timestamp + step) % step;
    }
}
